﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace FYPDemo
{
    public partial class RedeemReward : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["page"] = "reward";
                if (Session["username"] != null)
                {
                    btnRedeem.Enabled = true;
                }

                DataTable dt = new DataTable();
                int ID = Convert.ToInt32(Request.QueryString["productID"]);
                ViewState["productID"] = ID;
                dt = Reward_Module.showRewardProduct(ID);
                if (dt.Rows.Count > 0)
                {
                    prodImage.ImageUrl = dt.Rows[0][5].ToString();
                    lblProductName.Text = dt.Rows[0][1].ToString();
                    lblPoint.Text = dt.Rows[0][2].ToString();
                    ViewState["Point"] = dt.Rows[0][2];
                }

                if (Session["username"] != null)
                {
                    int result = Reward_Module.getRewardPoint(Session["username"].ToString());
                    lblRewardPoint.Text = result.ToString();
                    ViewState["RewardPoint"] = result;
                }
            }
        }

        protected void btnRedeem_Click(object sender, EventArgs e)
        {
            int quantity = Convert.ToInt32(ddlQuantity.SelectedValue);

            int totalPoint = Convert.ToInt32(ViewState["Point"]) * quantity;
            int latestPoint = 0;
            int result = 0;
            int result2 = 0;

            if (Convert.ToInt32(ViewState["RewardPoint"]) > totalPoint)
            {
                latestPoint = Convert.ToInt32(ViewState["RewardPoint"]) - totalPoint;

                result = Reward_Module.updateRewardPoint((Session["username"].ToString()), latestPoint);

                if (result > 0)
                {
                    result2 = Reward_Module.insertRewardFlow((Session["username"].ToString()), latestPoint, "OUT", Convert.ToInt32(ViewState["productID"]), Convert.ToInt32(ViewState["Point"]));
                    MessageBox.Show("Successfully redeem the reward item(s).");
                }

                else
                {
                    MessageBox.Show("There are some errors occur during the redeem process. Please try again later or contact our support team.");
                }
            }

            else
            {
                MessageBox.Show("Please make sure that your point is sufficient to redeem the reward item(s).");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Reward.aspx");
        }
    }
}